import request from '@/utils/request'

// Ansible配置管理API
export const ansibleAPI = {
  // 配置管理
  getConfigs() {
    return request({
      url: '/api/ansible/configs/',
      method: 'get'
    })
  },

  getConfig(id: number) {
    return request({
      url: `/api/ansible/configs/${id}/`,
      method: 'get'
    })
  },

  createConfig(data: any) {
    return request({
      url: '/api/ansible/configs/',
      method: 'post',
      data
    })
  },

  updateConfig(id: number, data: any) {
    return request({
      url: `/api/ansible/configs/${id}/`,
      method: 'put',
      data
    })
  },

  deleteConfig(id: number) {
    return request({
      url: `/api/ansible/configs/${id}/`,
      method: 'delete'
    })
  },

  testConnection(id: number) {
    return request({
      url: `/api/ansible/configs/${id}/test_connection/`,
      method: 'post'
    })
  },

  switchConfig(id: number) {
    return request({
      url: `/api/ansible/configs/${id}/switch_config/`,
      method: 'post'
    })
  },

  healthCheck() {
    return request({
      url: '/api/ansible/configs/health_check_all/',
      method: 'post'
    })
  },

  // 获取Ansible配置管理的主机列表
  getConfigHosts(configId: number) {
    return request({
      url: `/api/ansible/configs/${configId}/hosts/`,
      method: 'get'
    })
  },

  // 模块管理
  searchModules(params: any) {
    return request({
      url: '/api/ansible/modules/search/',
      method: 'get',
      params
    })
  },

  getModuleInfo(moduleName: string) {
    return request({
      url: `/api/ansible/modules/${moduleName}/`,
      method: 'get'
    })
  },

  getModuleParameters(moduleName: string) {
    return request({
      url: `/api/ansible/modules/${moduleName}/parameters/`,
      method: 'get'
    })
  },

  getModuleCategories() {
    return request({
      url: '/api/ansible/modules/categories/',
      method: 'get'
    })
  },

  suggestArgs(params: any) {
    return request({
      url: '/api/ansible/modules/suggest_args/',
      method: 'get',
      params
    })
  },

  // Inventory管理
  getInventories(params?: any) {
    return request({
      url: '/api/ansible/inventories/',
      method: 'get',
      params
    })
  },

  getHostsAndGroups(inventoryId?: number) {
    return request({
      url: '/api/inventory/hosts-and-groups/',
      method: 'get',
      params: inventoryId ? { inventory_id: inventoryId } : {}
    })
  },

  suggestHostPatterns(params: any) {
    return request({
      url: '/api/ansible/inventories/suggest_patterns/',
      method: 'get',
      params
    })
  },

  // 执行管理
  runAdhoc(data: any) {
    return request({
      url: '/api/ansible/jobs/run_adhoc/',
      method: 'post',
      data
    })
  },

  runPlaybook(data: any) {
    return request({
      url: '/api/ansible/jobs/run_playbook/',
      method: 'post',
      data
    })
  },

  getJobStatus(jobId: string) {
    return request({
      url: `/api/ansible/jobs/${jobId}/get_status/`,
      method: 'get'
    })
  },

  getExecutionLogs(params: any) {
    return request({
      url: '/api/ansible/jobs/',
      method: 'get',
      params
    })
  },

  cancelJob(jobId: string) {
    return request({
      url: `/api/ansible/jobs/${jobId}/cancel/`,
      method: 'post'
    })
  },

  relaunchJob(jobId: string) {
    return request({
      url: `/api/ansible/jobs/${jobId}/relaunch/`,
      method: 'post'
    })
  },

  // 凭据管理
  getCredentials(params?: any) {
    return request({
      url: '/api/ansible/credentials/',
      method: 'get',
      params
    })
  },

  createCredential(data: any) {
    return request({
      url: '/api/ansible/credentials/',
      method: 'post',
      data
    })
  },

  updateCredential(id: number, data: any) {
    return request({
      url: `/api/ansible/credentials/${id}/`,
      method: 'put',
      data
    })
  },

  deleteCredential(id: number) {
    return request({
      url: `/api/ansible/credentials/${id}/`,
      method: 'delete'
    })
  },

  getCredentialDetail(id: number) {
    return request({
      url: `/api/ansible/credentials/${id}/`,
      method: 'get'
    })
  },

  testCredential(id: number) {
    return request({
      url: `/api/ansible/credentials/${id}/test_credential/`,
      method: 'post'
    })
  },

  testAllCredentials() {
    return request({
      url: '/api/ansible/credentials/test_all_credentials/',
      method: 'post'
    })
  },

  // 推送公钥
  pushPublicKey(id: number, data: any) {
    return request({
      url: `/api/ansible/credentials/${id}/push_public_key/`,
      method: 'post',
      data
    })
  },

  // 获取推送目标（主机和主机组）
  getPushTargets() {
    return request({
      url: '/api/ansible/credentials/get_push_targets/',
      method: 'get'
    })
  },

  // Playbook文件管理
  getHostDirectories(hostId: number, path?: string) {
    return request({
      url: `/api/ansible/hosts/${hostId}/directories/`,
      method: 'get',
      params: { path }
    })
  },

  getPlaybookFiles(hostId: number, path: string) {
    return request({
      url: `/api/ansible/hosts/${hostId}/playbook-files/`,
      method: 'get',
      params: { path }
    })
  },

  getPlaybookContent(hostId: number, filePath: string) {
    return request({
      url: `/api/ansible/hosts/${hostId}/playbook-content/`,
      method: 'get',
      params: { file_path: filePath }
    })
  },

  savePlaybookContent(hostId: number, filePath: string, content: string) {
    return request({
      url: `/api/ansible/hosts/${hostId}/playbook-content/`,
      method: 'post',
      data: { file_path: filePath, content }
    })
  },

  // Playbook执行
  executePlaybook(data: any) {
    return request({
      url: '/api/ansible/jobs/execute_playbook/',
      method: 'post',
      data
    })
  },

  // 目录浏览和文件操作
  getDirectoryContent(hostId: number, path: string) {
    return request({
      url: `/api/ansible/hosts/${hostId}/directories/`,
      method: 'get',
      params: { path }
    })
  },

  getFileContent(hostId: number, filePath: string) {
    return request({
      url: `/api/ansible/hosts/${hostId}/playbook-content/`,
      method: 'get',
      params: { file_path: filePath }
    })
  },

  saveFileContent(hostId: number, filePath: string, content: string) {
    return request({
      url: `/api/ansible/hosts/${hostId}/playbook-content/`,
      method: 'post',
      data: {
        file_path: filePath,
        content: content
      }
    })
  }
}

export default ansibleAPI
